// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of DOM3XPath
//
// DOM3XPath is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// DOM3XPath is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with DOM3XPath. If not, see <http://www.gnu.org/licenses/>.
//	
//
//   $Id$
// XPathExpression.h : Declaration of the CXPathExpression

#ifndef __XPATHEXPRESSION_H_
#define __XPATHEXPRESSION_H_

#include "resource.h"       // main symbols
#include "XPathResult.h"
#include "XPathHelper.h"

//xsl wrapper
class CXSLTemplate
{
public:
	CXSLTemplate();
	virtual ~CXSLTemplate();

// [TODO]
// This is temporarily public, until we add more methods.
//private:
	CComPtr<IXSLTemplate> m_pXSLTemplate;
};
//#define COUNT_EXPRESSION 1
/////////////////////////////////////////////////////////////////////////////
// CXPathExpression
class ATL_NO_VTABLE CXPathExpression : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CXPathExpression, &CLSID_XPathExpression>,
	public ISupportErrorInfo,
	public IDispatchImpl<IXPathExpression, &IID_IXPathExpression, &LIBID_DOM3XPATHLib, 1, 1>
{

public:
	CXPathExpression():
		m_pHelper(0)
	{
		#ifdef COUNT_EXPRESSION
			++sm_Count;	
		#endif
	}
	virtual ~CXPathExpression()
	{
		#ifdef COUNT_EXPRESSION
			--sm_Count;	
		#endif
		ATLTRACE("Destroying CXPathExpression\n");
		//m_pProcessor always Releases to 1, if I
		//	perform an extra release, the release 
		//	from 1 fails???  I would expect the 
		//	extra release to cause a problem, 
		//	but not here.

		if(m_pProcessor)
		{
			//allow the default function library to be removed.
			m_pProcessor->addObject(0,L"http://www.x-port.net/namespaces/DOM3XPath");
			CComVariant v;
			v.vt = VT_DISPATCH;
			v.pdispVal = NULL;
			HRESULT hr = m_pProcessor->put_input(v);
			m_pProcessor.Release();
		}

		m_pResolver.Release();
		::safeRelease(m_pHelper);
	}

		#ifdef COUNT_EXPRESSION
			static unsigned long sm_Count;	
		#endif

DECLARE_REGISTRY_RESOURCEID(IDR_XPATHEXPRESSION)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CXPathExpression)
	COM_INTERFACE_ENTRY(IXPathExpression)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IXPathExpression
public:
	STDMETHOD(addFunctionLibrary)(IDispatch * pObj,BSTR sURI, BOOL bIsDefaultLibrary);
	STDMETHOD(evaluate)(IXMLDOMNode * contextNode,short type, IXPathResult * result, IXPathResult ** retVal);

//public - non-com
	HRESULT initialise(BSTR sExpr, IXPathNSResolver * pResolver);

private:
	CComQIPtr<IXPathNSResolver> m_pResolver;
	CComQIPtr<IXSLProcessor> m_pProcessor;
	HRESULT transformByStylesheet(IXMLDOMNode* pContextNode, CComObject<CXPathResult>* pResult);
	static inline void parseAttributeString(char  *  const sNSList,IXMLDOMElement * pExprEl);
//	CXSLTemplate oTemplate;
	CComObject<CXPathHelper> *m_pHelper;
	HRESULT QualifyUnqualifiedFunctions(const BSTR bsExpr, BSTR * bsModifiedExpr) const;
	HRESULT GenerateCachedStylesheet(const CComBSTR & bstrQualifiedExpression);
	CComBSTR m_sExpression;
};


#endif //__XPATHEXPRESSION_H_
